package com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.dbbean.AnswerContent;
import com.ciwong.epaper.modules.dbbean.LswAnswer;
import com.ciwong.epaper.modules.epaper.bean.ModuleSet;
import com.ciwong.epaper.modules.epaper.bean.OptionInfo;
import com.ciwong.epaper.modules.epaper.bean.QuestionScore;
import com.ciwong.epaper.modules.epaper.bean.Questions;
import com.ciwong.epaper.modules.epaper.bean.ResourceDetail;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.LsBottomView;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.adapter.ShortcoversationAdapter;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.adapter.ShortcoversationAdapter.OnClickForAnswerListener;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.manager.ScrollSpeedLinearLayoutManger;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.mobilelib.widget.ProgessBeakPoint;

import java.util.ArrayList;
import java.util.List;

import static com.ciwong.epaper.modules.epaper.bean.OptionInfo.IsAnswer.IS_ANSWER_CORRECT;

/**
 * date :2018/5/3
 * author: loco
 * description:
 * version:
 */

public class ShortCoversationViewFragment extends ShortConversationFragment {

    protected RecyclerView rvShortConversation;
    protected ShortcoversationAdapter shortDialogAdapter;

    protected LsBottomView lsBottomView;
    protected TextView bottomNo;
    protected ProgessBeakPoint bottomProgress;
    private ScrollSpeedLinearLayoutManger scrollSpeedLinearLayoutManger;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_short_conversation, container, false);
        return rootView;
    }

    private void initView(View rootView) {
        lsBottomView = rootView.findViewById(R.id.ls_bottom_view);
        bottomProgress = rootView.findViewById(R.id.bottom_progress);
        bottomNo = rootView.findViewById(R.id.bottom_no);
        rvShortConversation = rootView.findViewById(R.id.rv_short_dialogue);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mResourceDetail = initData(mResourceDetail);
        initEvent();
    }

    private void initEvent() {
        initRecycleView();
        lsBottomView.setOnSkipListener(mSkipListener);
        if (mResourceDetail != null) {
            startPlayQuestion(lsBottomView);
        }
    }

    /**
     * 保存每个题的答案
     *
     * @param mResourceDetail
     * @return
     */
    private ResourceDetail initData(ResourceDetail mResourceDetail) {
        if (mHasDoneAnswers != null && mHasDoneAnswers.size() > 0 && mLsMode == ListenSpeakUtil.LsMode.RETRY_MODE)
            mLswAnswerList = mHasDoneAnswers;
        if (mResourceDetail == null) return mResourceDetail;
        List<Questions> questions = mResourceDetail.getQuestions();
        List<QuestionScore> scores = mResourceDetail.getScores();
        for (Questions mQuestion : questions) {
            LswAnswer lswAnswer = creatOptionAnswer(mQuestion, scores, mUuid);
            boolean contains = mLswAnswerList.contains(lswAnswer);
            if (!contains)
                mLswAnswerList.add(lswAnswer);
            else{
                for (LswAnswer lswA:mLswAnswerList){
                    if (lswA.getVersionId().equals(lswAnswer.getVersionId()))
                        mQuestion.setLswAnswer(lswA);
                }
            }
        }
        return mResourceDetail;
    }

    private void initRecycleView() {
        scrollSpeedLinearLayoutManger = new ScrollSpeedLinearLayoutManger(mContext, LinearLayoutManager.VERTICAL, false);
        rvShortConversation.setLayoutManager(scrollSpeedLinearLayoutManger);
        shortDialogAdapter = new ShortcoversationAdapter(mContext);
        rvShortConversation.setAdapter(shortDialogAdapter);
        shortDialogAdapter.setOnClickForAnswerListener(new OnClickForAnswerListener() {
            @Override
            public void onclickForAnswer(LswAnswer lswAnswer, int position) {//用集合是为了兼容多选
                if (lswAnswer == null) return;//未选择
                if (mLswAnswerList.contains(lswAnswer))
                    mLswAnswerList.set(mLswAnswerList.indexOf(lswAnswer), lswAnswer);
                else mLswAnswerList.add(lswAnswer);
            }
        });
    }

    private LswAnswer creatOptionAnswer(Questions question, List<QuestionScore> scores, String uuid) {
        LswAnswer mlswAnswer = new LswAnswer();
        mlswAnswer.setQtype(question.getType());
        mlswAnswer.setWorkSaveUUid(uuid);
        mlswAnswer.setVersionId(question.getVersionId());
        if (question.getType() == Questions.QuestionType.QUESTION_TYPE_CHOICE) {
            mlswAnswer.setAnswerType(Questions.AnswerType.QUESTION_TYPE_CHOICE);
        }
        mlswAnswer.setIs_objective(question.getIs_objective());
        //选择填空不需要上传，默认成功
        mlswAnswer.setUploadState(LswAnswer.SUCCESS);
        for (QuestionScore q : scores) {
            if (q.getQuestionVersionId().equals(question.getVersionId())) {
                mlswAnswer.setRefScore(q.getScore());
            }
        }
        List<AnswerContent> answerContents = new ArrayList<>();
        AnswerContent answerContent = new AnswerContent();

        //找出正确答案ID  只适用单选
        if (null != question.getOptions() && question.getOptions().size() > 0) {
            List<OptionInfo> options = question.getOptions();
            for (OptionInfo o : options) {
                //正确答案
                if (o.getIsAnswer() == IS_ANSWER_CORRECT) {
                    answerContent.setRefAnswer(o.getId());
//                    answerContent.setContent(o.getId());
                }
            }
        }
        answerContent.setContent("");//未作答的状态
        answerContents.add(answerContent);
        mlswAnswer.setAnswerContents(answerContents);
        mlswAnswer.setAnswer(false);//未作答的状态
        question.setLswAnswer(mlswAnswer);
        return mlswAnswer;
    }

    @Override
    protected void setData(ModuleSet moduleSet, List<Questions> questionsList) {
        shortDialogAdapter.setData(moduleSet, questionsList);
    }

    @Override
    protected void checkHeighlightPosition(int position) {

        shortDialogAdapter.setHightLight(position);
        rvShortConversation.smoothScrollToPosition(position);
    }
    @Override
    protected void showPlayButton(int questionTypeChoice) {
        lsBottomView.showPlayButton(Questions.QuestionType.QUESTION_TYPE_CHOICE);
    }

    @Override
    protected void timeStart(int duration) {
        lsBottomView.seekBarSetMax(duration);
    }

    @Override
    protected void timeUpdate(CharSequence sequence, int progress, boolean showSkipBtn, int skipType, ListenSpeakUtil.State status, boolean isLastQuestion) {
        lsBottomView.updateTimer(sequence, progress, showSkipBtn, skipType, status, isLastQuestion);
    }

    @Override
    protected void timeEnd() {
        lsBottomView.hideTimerAnimation();
    }

    @Override
    protected void timeOverForNextTip(int visible) {
        lsBottomView.setNextTipsVisibility(visible);
    }

    @Override
    protected void timeOverForAnswer(int visible, boolean bool, int i) {
        lsBottomView.setStateBarVisibility(visible);
        lsBottomView.setRecordState(bool, -1);
    }

    @Override
    protected void onPlayStop(boolean bool, int invisible) {
        lsBottomView.setPlayBtnState(bool);
        lsBottomView.setNextTipsVisibility(invisible);
    }
}



